﻿Imports System.Collections.Specialized
Imports System.Text.RegularExpressions
Imports System.Text

Public Class OutlookArgus

    '*********************************************************************************************************************
    '
    '             Class:  OutlookArgus
    '      Initial Date:  04/10/2008
    '      Last Updated:  05/21/2008
    '     Programmer(s):  Blake Pell, bpell@indiana.edu
    '       Description:  Outlook Add-in to facilitate looking for possible sensitive data in an outgoing e-mail and 
    '                     alerting the user to it, then having them confirm whether to continue.  This isn't meant to 
    '                     stop anybody from sending e-mails but rather to call their attention to patterns that they may
    '                     want to think twice about sending (like, credit card numbers or social security numbers).
    '  Additional Notes:  N/A
    '
    '*********************************************************************************************************************

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

    End Sub

    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub

    Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend

        Dim Mail As Microsoft.Office.Interop.Outlook.MailItem

        Try
            Mail = CType(Item, Microsoft.Office.Interop.Outlook.MailItem)
        Catch ex As Exception
            Exit Sub
        End Try

        Dim Sb As New StringBuilder
        Dim Sd As New StringDictionary
        Dim PatternFound As Boolean = False
        Sd.Add("Credit Card Pattern", "\b\d{4}\s?-?\d{4}\s?-?\d{4}\s?-?\d{4}\b")
        Sd.Add("Social Security Pattern", "\b\d{3}\s?-?\d{2}\s?-?\d{4}\b")

        Sb.AppendFormat("The following pattern or patterns have been identified in your message:{0}{1}", vbCrLf, vbCrLf)

        For Each Key As String In Sd.Keys
            Dim RegEx As New Regex(Sd(Key))
            Dim Mc As MatchCollection = RegEx.Matches(Mail.Body)

            If Mc.Count > 0 Then
                If Mc.Count > 1 Then
                    Sb.AppendFormat("{0}: {1} matches ({2}).{3}", Key, Mc.Count, MatchToString(Mc), vbCrLf)
                Else
                    Sb.AppendFormat("{0}: {1} match ({2}).{3}", Key, Mc.Count, MatchToString(Mc), vbCrLf)
                End If
                PatternFound = True
            End If

        Next

        If PatternFound = True Then

            Sb.AppendLine("")
            Sb.Append("Pattern matching may provide false positives in cases where a number is similiar to secure information.  If your ")
            Sb.Append("message does not include secure information you can press 'Yes' to continue sending this message.  Otherwise, you ")
            Sb.Append("can press 'No' and you will be taken back to your message to review. ")
            Sb.AppendLine(vbCrLf)
            Sb.Append("Press 'Yes' to continue sending your message or 'No' to return to your message without sending. ")

            Dim Answer As Integer = Windows.Forms.MessageBox.Show(Sb.ToString, "Outlook Argus - Security Verification", Windows.Forms.MessageBoxButtons.YesNoCancel, Windows.Forms.MessageBoxIcon.Question, Windows.Forms.MessageBoxDefaultButton.Button3)

            If Answer = vbYes Then
                Cancel = False
            Else
                Cancel = True
            End If
        End If

    End Sub

    Public Function MatchToString(ByVal Mc As MatchCollection) As String
        Dim Sb As New StringBuilder

        For X As Integer = 0 To Mc.Count - 1
            Sb.AppendFormat("{0}{1}", Mc(X).ToString, ", ")
        Next

        If Sb.ToString.Length > 2 Then
            Return Left(Sb.ToString, Sb.ToString.Length - 2)
        Else
            Return Sb.ToString
        End If

    End Function

End Class
